// $ANTLR 3.0.1 C:\\Users\\Dwight Schrute\\workspace\\cs214\\CS-214-Interpreters\\src\\edu\\calvin\\djs32\\cs214\\interpreters\\frontend\\Hobbes2008TIRBuilder.g 2008-05-12 02:09:24

package edu.calvin.djs32.cs214.interpreters.frontend;
import edu.calvin.cs214.hobbes2008.*;


import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;import java.util.Stack;
import java.util.List;
import java.util.ArrayList;

@SuppressWarnings("all")
public class Hobbes2008TIRBuilder extends TreeParser {
    public static final String[] tokenNames = new String[] {
        "<invalid>", "<EOR>", "<DOWN>", "<UP>", "TUPLE", "APPLICATION", "IF", "ELSE", "THEN", "END", "LET", "IN", "KEYWORD", "LETFUNC", "NUMBER", "HEXDIGIT", "SIGN", "INTEGER", "IDENTIFIER", "WHITESPACE", "COMMENTS", "STRING", "COMMA", "LPAREN", "RPAREN"
    };
    public static final int INTEGER=17;
    public static final int SIGN=16;
    public static final int KEYWORD=12;
    public static final int APPLICATION=5;
    public static final int ELSE=7;
    public static final int NUMBER=14;
    public static final int WHITESPACE=19;
    public static final int LETFUNC=13;
    public static final int EOF=-1;
    public static final int LPAREN=23;
    public static final int IF=6;
    public static final int RPAREN=24;
    public static final int TUPLE=4;
    public static final int THEN=8;
    public static final int IN=11;
    public static final int COMMENTS=20;
    public static final int COMMA=22;
    public static final int IDENTIFIER=18;
    public static final int END=9;
    public static final int LET=10;
    public static final int STRING=21;
    public static final int HEXDIGIT=15;

        public Hobbes2008TIRBuilder(TreeNodeStream input) {
            super(input);
        }
        

    public String[] getTokenNames() { return tokenNames; }
    public String getGrammarFileName() { return "C:\\Users\\Dwight Schrute\\workspace\\cs214\\CS-214-Interpreters\\src\\edu\\calvin\\djs32\\cs214\\interpreters\\frontend\\Hobbes2008TIRBuilder.g"; }

     
      protected void mismatch(IntStream input, int ttype, BitSet follow) 
        throws RecognitionException { 
        throw new MismatchedTokenException(ttype, input); 
      } 
      public void recoverFromMismatchedSet(IntStream input, RecognitionException e, BitSet follow) 
        throws RecognitionException { 
        throw e; 
      } 



    // $ANTLR start expression
    // C:\\Users\\Dwight Schrute\\workspace\\cs214\\CS-214-Interpreters\\src\\edu\\calvin\\djs32\\cs214\\interpreters\\frontend\\Hobbes2008TIRBuilder.g:30:1: expression returns [HobbesTIR tir] : ( INTEGER | STRING | t= tuple | ^( APPLICATION (id= identifier t= tuple ) ) | ^( IF ex1= expression ex2= expression ex3= expression ) | i= identifier | ^( LET i= identifier ex1= expression ex2= expression ) | ^( LETFUNC i1= identifier i2= identifier ex1= expression ex2= expression ) );
    public final HobbesTIR expression() throws RecognitionException {
        HobbesTIR tir = null;

        CommonTree INTEGER1=null;
        CommonTree STRING2=null;
        TupleTIR t = null;

        IdentifierTIR id = null;

        HobbesTIR ex1 = null;

        HobbesTIR ex2 = null;

        HobbesTIR ex3 = null;

        IdentifierTIR i = null;

        IdentifierTIR i1 = null;

        IdentifierTIR i2 = null;


        
                List expressions = new ArrayList<HobbesTIR>();

        try {
            // C:\\Users\\Dwight Schrute\\workspace\\cs214\\CS-214-Interpreters\\src\\edu\\calvin\\djs32\\cs214\\interpreters\\frontend\\Hobbes2008TIRBuilder.g:34:9: ( INTEGER | STRING | t= tuple | ^( APPLICATION (id= identifier t= tuple ) ) | ^( IF ex1= expression ex2= expression ex3= expression ) | i= identifier | ^( LET i= identifier ex1= expression ex2= expression ) | ^( LETFUNC i1= identifier i2= identifier ex1= expression ex2= expression ) )
            int alt1=8;
            switch ( input.LA(1) ) {
            case INTEGER:
                {
                alt1=1;
                }
                break;
            case STRING:
                {
                alt1=2;
                }
                break;
            case TUPLE:
                {
                alt1=3;
                }
                break;
            case APPLICATION:
                {
                alt1=4;
                }
                break;
            case IF:
                {
                alt1=5;
                }
                break;
            case IDENTIFIER:
                {
                alt1=6;
                }
                break;
            case LET:
                {
                alt1=7;
                }
                break;
            case LETFUNC:
                {
                alt1=8;
                }
                break;
            default:
                NoViableAltException nvae =
                    new NoViableAltException("30:1: expression returns [HobbesTIR tir] : ( INTEGER | STRING | t= tuple | ^( APPLICATION (id= identifier t= tuple ) ) | ^( IF ex1= expression ex2= expression ex3= expression ) | i= identifier | ^( LET i= identifier ex1= expression ex2= expression ) | ^( LETFUNC i1= identifier i2= identifier ex1= expression ex2= expression ) );", 1, 0, input);

                throw nvae;
            }

            switch (alt1) {
                case 1 :
                    // C:\\Users\\Dwight Schrute\\workspace\\cs214\\CS-214-Interpreters\\src\\edu\\calvin\\djs32\\cs214\\interpreters\\frontend\\Hobbes2008TIRBuilder.g:34:11: INTEGER
                    {
                    INTEGER1=(CommonTree)input.LT(1);
                    match(input,INTEGER,FOLLOW_INTEGER_in_expression84); 
                     String intstr = ((INTEGER1.getText().charAt(0) == '+') ? INTEGER1.getText().substring(1) : INTEGER1.getText()); tir = new IntegerTIR(Integer.decode(intstr)); 

                    }
                    break;
                case 2 :
                    // C:\\Users\\Dwight Schrute\\workspace\\cs214\\CS-214-Interpreters\\src\\edu\\calvin\\djs32\\cs214\\interpreters\\frontend\\Hobbes2008TIRBuilder.g:35:11: STRING
                    {
                    STRING2=(CommonTree)input.LT(1);
                    match(input,STRING,FOLLOW_STRING_in_expression98); 
                     tir = new StringTIR(STRING2.getText().substring(1, (STRING2.getText().length()-1))); 

                    }
                    break;
                case 3 :
                    // C:\\Users\\Dwight Schrute\\workspace\\cs214\\CS-214-Interpreters\\src\\edu\\calvin\\djs32\\cs214\\interpreters\\frontend\\Hobbes2008TIRBuilder.g:36:11: t= tuple
                    {
                    pushFollow(FOLLOW_tuple_in_expression116);
                    t=tuple();
                    _fsp--;

                    tir = t;

                    }
                    break;
                case 4 :
                    // C:\\Users\\Dwight Schrute\\workspace\\cs214\\CS-214-Interpreters\\src\\edu\\calvin\\djs32\\cs214\\interpreters\\frontend\\Hobbes2008TIRBuilder.g:37:11: ^( APPLICATION (id= identifier t= tuple ) )
                    {
                    match(input,APPLICATION,FOLLOW_APPLICATION_in_expression131); 

                    match(input, Token.DOWN, null); 
                    // C:\\Users\\Dwight Schrute\\workspace\\cs214\\CS-214-Interpreters\\src\\edu\\calvin\\djs32\\cs214\\interpreters\\frontend\\Hobbes2008TIRBuilder.g:37:25: (id= identifier t= tuple )
                    // C:\\Users\\Dwight Schrute\\workspace\\cs214\\CS-214-Interpreters\\src\\edu\\calvin\\djs32\\cs214\\interpreters\\frontend\\Hobbes2008TIRBuilder.g:37:26: id= identifier t= tuple
                    {
                    pushFollow(FOLLOW_identifier_in_expression138);
                    id=identifier();
                    _fsp--;

                    pushFollow(FOLLOW_tuple_in_expression144);
                    t=tuple();
                    _fsp--;


                    }


                    match(input, Token.UP, null); 
                    tir = new ApplicationTIR(id, t); 

                    }
                    break;
                case 5 :
                    // C:\\Users\\Dwight Schrute\\workspace\\cs214\\CS-214-Interpreters\\src\\edu\\calvin\\djs32\\cs214\\interpreters\\frontend\\Hobbes2008TIRBuilder.g:38:11: ^( IF ex1= expression ex2= expression ex3= expression )
                    {
                    match(input,IF,FOLLOW_IF_in_expression164); 

                    match(input, Token.DOWN, null); 
                    pushFollow(FOLLOW_expression_in_expression168);
                    ex1=expression();
                    _fsp--;

                    pushFollow(FOLLOW_expression_in_expression172);
                    ex2=expression();
                    _fsp--;

                    pushFollow(FOLLOW_expression_in_expression176);
                    ex3=expression();
                    _fsp--;


                    match(input, Token.UP, null); 
                    tir = new IfTIR(ex1, ex2, ex3); 

                    }
                    break;
                case 6 :
                    // C:\\Users\\Dwight Schrute\\workspace\\cs214\\CS-214-Interpreters\\src\\edu\\calvin\\djs32\\cs214\\interpreters\\frontend\\Hobbes2008TIRBuilder.g:39:11: i= identifier
                    {
                    pushFollow(FOLLOW_identifier_in_expression195);
                    i=identifier();
                    _fsp--;

                    tir = i;

                    }
                    break;
                case 7 :
                    // C:\\Users\\Dwight Schrute\\workspace\\cs214\\CS-214-Interpreters\\src\\edu\\calvin\\djs32\\cs214\\interpreters\\frontend\\Hobbes2008TIRBuilder.g:40:11: ^( LET i= identifier ex1= expression ex2= expression )
                    {
                    match(input,LET,FOLLOW_LET_in_expression210); 

                    match(input, Token.DOWN, null); 
                    pushFollow(FOLLOW_identifier_in_expression214);
                    i=identifier();
                    _fsp--;

                    pushFollow(FOLLOW_expression_in_expression218);
                    ex1=expression();
                    _fsp--;

                    pushFollow(FOLLOW_expression_in_expression222);
                    ex2=expression();
                    _fsp--;


                    match(input, Token.UP, null); 
                    tir = new LetTIR(i, ex1, ex2); 

                    }
                    break;
                case 8 :
                    // C:\\Users\\Dwight Schrute\\workspace\\cs214\\CS-214-Interpreters\\src\\edu\\calvin\\djs32\\cs214\\interpreters\\frontend\\Hobbes2008TIRBuilder.g:41:11: ^( LETFUNC i1= identifier i2= identifier ex1= expression ex2= expression )
                    {
                    match(input,LETFUNC,FOLLOW_LETFUNC_in_expression238); 

                    match(input, Token.DOWN, null); 
                    pushFollow(FOLLOW_identifier_in_expression242);
                    i1=identifier();
                    _fsp--;

                    pushFollow(FOLLOW_identifier_in_expression246);
                    i2=identifier();
                    _fsp--;

                    pushFollow(FOLLOW_expression_in_expression250);
                    ex1=expression();
                    _fsp--;

                    pushFollow(FOLLOW_expression_in_expression254);
                    ex2=expression();
                    _fsp--;


                    match(input, Token.UP, null); 
                    LambdaTIR functionLambda = new LambdaTIR(i2, ex1);
                            	tir = new LetFunctionTIR(i1, functionLambda, ex2);

                    }
                    break;

            }
        }
         
          catch (RecognitionException e) { 
            throw e; 
          } 
        finally {
        }
        return tir;
    }
    // $ANTLR end expression


    // $ANTLR start tuple
    // C:\\Users\\Dwight Schrute\\workspace\\cs214\\CS-214-Interpreters\\src\\edu\\calvin\\djs32\\cs214\\interpreters\\frontend\\Hobbes2008TIRBuilder.g:46:1: tuple returns [TupleTIR tuple] : ^( TUPLE (ex= expression )* ) ;
    public final TupleTIR tuple() throws RecognitionException {
        TupleTIR tuple = null;

        HobbesTIR ex = null;


        
                List expressions = new ArrayList<HobbesTIR>();

        try {
            // C:\\Users\\Dwight Schrute\\workspace\\cs214\\CS-214-Interpreters\\src\\edu\\calvin\\djs32\\cs214\\interpreters\\frontend\\Hobbes2008TIRBuilder.g:50:2: ( ^( TUPLE (ex= expression )* ) )
            // C:\\Users\\Dwight Schrute\\workspace\\cs214\\CS-214-Interpreters\\src\\edu\\calvin\\djs32\\cs214\\interpreters\\frontend\\Hobbes2008TIRBuilder.g:50:3: ^( TUPLE (ex= expression )* )
            {
            match(input,TUPLE,FOLLOW_TUPLE_in_tuple286); 

            if ( input.LA(1)==Token.DOWN ) {
                match(input, Token.DOWN, null); 
                // C:\\Users\\Dwight Schrute\\workspace\\cs214\\CS-214-Interpreters\\src\\edu\\calvin\\djs32\\cs214\\interpreters\\frontend\\Hobbes2008TIRBuilder.g:50:11: (ex= expression )*
                loop2:
                do {
                    int alt2=2;
                    int LA2_0 = input.LA(1);

                    if ( ((LA2_0>=TUPLE && LA2_0<=IF)||LA2_0==LET||LA2_0==LETFUNC||(LA2_0>=INTEGER && LA2_0<=IDENTIFIER)||LA2_0==STRING) ) {
                        alt2=1;
                    }


                    switch (alt2) {
                	case 1 :
                	    // C:\\Users\\Dwight Schrute\\workspace\\cs214\\CS-214-Interpreters\\src\\edu\\calvin\\djs32\\cs214\\interpreters\\frontend\\Hobbes2008TIRBuilder.g:50:13: ex= expression
                	    {
                	    pushFollow(FOLLOW_expression_in_tuple292);
                	    ex=expression();
                	    _fsp--;

                	     expressions.add(ex); 

                	    }
                	    break;

                	default :
                	    break loop2;
                    }
                } while (true);


                match(input, Token.UP, null); 
            }
             tuple = new TupleTIR(expressions); 

            }

        }
         
          catch (RecognitionException e) { 
            throw e; 
          } 
        finally {
        }
        return tuple;
    }
    // $ANTLR end tuple


    // $ANTLR start identifier
    // C:\\Users\\Dwight Schrute\\workspace\\cs214\\CS-214-Interpreters\\src\\edu\\calvin\\djs32\\cs214\\interpreters\\frontend\\Hobbes2008TIRBuilder.g:53:1: identifier returns [IdentifierTIR tir] : IDENTIFIER ;
    public final IdentifierTIR identifier() throws RecognitionException {
        IdentifierTIR tir = null;

        CommonTree IDENTIFIER3=null;

        try {
            // C:\\Users\\Dwight Schrute\\workspace\\cs214\\CS-214-Interpreters\\src\\edu\\calvin\\djs32\\cs214\\interpreters\\frontend\\Hobbes2008TIRBuilder.g:54:3: ( IDENTIFIER )
            // C:\\Users\\Dwight Schrute\\workspace\\cs214\\CS-214-Interpreters\\src\\edu\\calvin\\djs32\\cs214\\interpreters\\frontend\\Hobbes2008TIRBuilder.g:54:4: IDENTIFIER
            {
            IDENTIFIER3=(CommonTree)input.LT(1);
            match(input,IDENTIFIER,FOLLOW_IDENTIFIER_in_identifier316); 
            tir = new IdentifierTIR(IDENTIFIER3.getText()); 

            }

        }
         
          catch (RecognitionException e) { 
            throw e; 
          } 
        finally {
        }
        return tir;
    }
    // $ANTLR end identifier


 

    public static final BitSet FOLLOW_INTEGER_in_expression84 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_STRING_in_expression98 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_tuple_in_expression116 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_APPLICATION_in_expression131 = new BitSet(new long[]{0x0000000000000004L});
    public static final BitSet FOLLOW_identifier_in_expression138 = new BitSet(new long[]{0x0000000000000010L});
    public static final BitSet FOLLOW_tuple_in_expression144 = new BitSet(new long[]{0x0000000000000008L});
    public static final BitSet FOLLOW_IF_in_expression164 = new BitSet(new long[]{0x0000000000000004L});
    public static final BitSet FOLLOW_expression_in_expression168 = new BitSet(new long[]{0x0000000000262470L});
    public static final BitSet FOLLOW_expression_in_expression172 = new BitSet(new long[]{0x0000000000262470L});
    public static final BitSet FOLLOW_expression_in_expression176 = new BitSet(new long[]{0x0000000000000008L});
    public static final BitSet FOLLOW_identifier_in_expression195 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_LET_in_expression210 = new BitSet(new long[]{0x0000000000000004L});
    public static final BitSet FOLLOW_identifier_in_expression214 = new BitSet(new long[]{0x0000000000262470L});
    public static final BitSet FOLLOW_expression_in_expression218 = new BitSet(new long[]{0x0000000000262470L});
    public static final BitSet FOLLOW_expression_in_expression222 = new BitSet(new long[]{0x0000000000000008L});
    public static final BitSet FOLLOW_LETFUNC_in_expression238 = new BitSet(new long[]{0x0000000000000004L});
    public static final BitSet FOLLOW_identifier_in_expression242 = new BitSet(new long[]{0x0000000000040000L});
    public static final BitSet FOLLOW_identifier_in_expression246 = new BitSet(new long[]{0x0000000000262470L});
    public static final BitSet FOLLOW_expression_in_expression250 = new BitSet(new long[]{0x0000000000262470L});
    public static final BitSet FOLLOW_expression_in_expression254 = new BitSet(new long[]{0x0000000000000008L});
    public static final BitSet FOLLOW_TUPLE_in_tuple286 = new BitSet(new long[]{0x0000000000000004L});
    public static final BitSet FOLLOW_expression_in_tuple292 = new BitSet(new long[]{0x0000000000262478L});
    public static final BitSet FOLLOW_IDENTIFIER_in_identifier316 = new BitSet(new long[]{0x0000000000000002L});

}